import Vue from "vue";
import Router from "vue-router";
import Home from "./views/Home.vue";
import Login from "./views/Login.vue";


Vue.use(Router);

var router = new Router({
  routes: [
    {
      path: "/login", 
      component: Login
    },
    {
      path: "/home",
      component: Home
    },
  ]
});
//路由守卫
router.beforeEach(function(to, from, next){
   // 当前用户没有登录系统，并且还在访问非登录路由，就强制执行登录路由
  var token = window.sessionStorage.getItem('token')
  //用户没有登录系统(token===null)
  //该用户还要试图访问除了登录以外的其他页面(例如:home)
  //就强制该用户去登录
  if(token===null && to.path !== '/login'){
    return next('/login');
  }
  next() // 放行
}) 
export default router;

